// pages/annualSchedule/index.js
Page({
  data: {
    currentYear: new Date().getFullYear(),
    filterStatus: 'all',
    reminderType: 'notification',
    reminderTime: '1day',
    events: [],
    filteredEvents: [],
    upcomingCount: 0,
    completedCount: 0,
    totalCount: 0,
    importantEventsCount: 0,
    filingEventsCount: 0,
    paymentEventsCount: 0,
    policyEventsCount: 0
  },

  onLoad: function (options) {
    // 页面加载时初始化数据
    this.loadEvents();
    this.updateCounts();
  },

  onReady: function () {
    // 页面渲染完成后的操作
  },

  onShow: function () {
    // 页面显示时的操作
  },

  onHide: function () {
    // 页面隐藏时的操作
  },

  onUnload: function () {
    // 页面卸载时的操作
  },

  onPullDownRefresh: function () {
    // 用户下拉刷新时的操作
    this.loadEvents();
    this.updateCounts();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function () {
    // 用户上拉触底时的操作
  },

  onShareAppMessage: function () {
    // 用户点击右上角分享时的操作
  },

  // 加载事件数据
  loadEvents: function() {
    const year = this.data.currentYear;
    
    // 模拟年度税务事件数据
    const events = [
      {
        id: 1,
        date: year + '-01-15',
        title: '个人所得税专项附加扣除信息确认',
        description: '确认年度专项附加扣除信息，确保准确享受税收优惠',
        category: '重要',
        categoryClass: 'category-important',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 2,
        date: year + '-03-01',
        title: '年度个人所得税汇算清缴开始',
        description: '年度汇算清缴申报开始，核对收入和扣除项目',
        category: '申报',
        categoryClass: 'category-filing',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 3,
        date: year + '-04-30',
        title: '年度个人所得税汇算清缴截止',
        description: '年度汇算清缴申报截止，逾期将产生滞纳金',
        category: '重要',
        categoryClass: 'category-important',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 4,
        date: year + '-05-31',
        title: '企业所得税季度预缴申报',
        description: '第一季度企业所得税预缴申报截止',
        category: '申报',
        categoryClass: 'category-filing',
        status: 'completed',
        statusClass: 'status-completed',
        statusText: '已完成'
      },
      {
        id: 5,
        date: year + '-06-30',
        title: '企业所得税年度汇算清缴',
        description: '企业所得税年度汇算清缴截止',
        category: '重要',
        categoryClass: 'category-important',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 6,
        date: year + '-07-15',
        title: '增值税小规模纳税人申报',
        description: '第二季度增值税申报截止',
        category: '申报',
        categoryClass: 'category-filing',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 7,
        date: year + '-08-15',
        title: '个人所得税税款缴纳',
        description: '年度汇算清缴税款缴纳截止',
        category: '缴费',
        categoryClass: 'category-payment',
        status: 'overdue',
        statusClass: 'status-overdue',
        statusText: '已逾期'
      },
      {
        id: 8,
        date: year + '-09-30',
        title: '企业所得税季度预缴申报',
        description: '第三季度企业所得税预缴申报截止',
        category: '申报',
        categoryClass: 'category-filing',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 9,
        date: year + '-10-01',
        title: '新的税收优惠政策实施',
        description: '关注新实施的税收优惠政策，评估适用性',
        category: '政策',
        categoryClass: 'category-policy',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      },
      {
        id: 10,
        date: year + '-12-31',
        title: '年度税务资料归档',
        description: '整理归档全年税务资料，准备来年使用',
        category: '重要',
        categoryClass: 'category-important',
        status: 'upcoming',
        statusClass: 'status-upcoming',
        statusText: '待办'
      }
    ];

    this.setData({
      events: events,
      filteredEvents: events
    });
    
    this.applyFilter();
  },

  // 更新统计计数
  updateCounts: function() {
    const events = this.data.events;
    
    const upcomingCount = events.filter(event => event.status === 'upcoming').length;
    const completedCount = events.filter(event => event.status === 'completed').length;
    const totalCount = events.length;
    
    const importantEventsCount = events.filter(event => event.category === '重要').length;
    const filingEventsCount = events.filter(event => event.category === '申报').length;
    const paymentEventsCount = events.filter(event => event.category === '缴费').length;
    const policyEventsCount = events.filter(event => event.category === '政策').length;
    
    this.setData({
      upcomingCount: upcomingCount,
      completedCount: completedCount,
      totalCount: totalCount,
      importantEventsCount: importantEventsCount,
      filingEventsCount: filingEventsCount,
      paymentEventsCount: paymentEventsCount,
      policyEventsCount: policyEventsCount
    });
  },

  // 切换到上一年
  prevYear: function() {
    this.setData({
      currentYear: this.data.currentYear - 1
    });
    this.loadEvents();
    this.updateCounts();
  },

  // 切换到下一年
  nextYear: function() {
    this.setData({
      currentYear: this.data.currentYear + 1
    });
    this.loadEvents();
    this.updateCounts();
  },

  // 设置筛选条件
  onSetFilter: function(e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({
      filterStatus: filter
    });
    this.applyFilter();
  },

  // 应用筛选条件
  applyFilter: function() {
    const filter = this.data.filterStatus;
    const events = this.data.events;
    
    let filteredEvents = [];
    
    if (filter === 'all') {
      filteredEvents = events;
    } else {
      filteredEvents = events.filter(event => {
        if (filter === 'upcoming') return event.status === 'upcoming';
        if (filter === 'completed') return event.status === 'completed';
        if (filter === 'overdue') return event.status === 'overdue';
        return true;
      });
    }
    
    this.setData({
      filteredEvents: filteredEvents
    });
  },

  // 设置提醒
  onSetReminder: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '已设置提醒',
      icon: 'success'
    });
    
    // 这里可以添加实际的提醒设置逻辑
    console.log('设置提醒，事件ID：', id);
  },

  // 标记为完成
  onMarkComplete: function(e) {
    const id = e.currentTarget.dataset.id;
    
    // 更新事件状态
    const events = this.data.events.map(event => {
      if (event.id === id) {
        return Object.assign({}, event, {
          status: 'completed',
          statusClass: 'status-completed',
          statusText: '已完成'
        });
      }
      return event;
    });
    
    this.setData({
      events: events
    });
    
    this.applyFilter();
    this.updateCounts();
    
    wx.showToast({
      title: '标记为已完成',
      icon: 'success'
    });
  },

  // 撤销完成状态
  onUndoComplete: function(e) {
    const id = e.currentTarget.dataset.id;
    
    // 更新事件状态
    const events = this.data.events.map(event => {
      if (event.id === id) {
        return Object.assign({}, event, {
          status: 'upcoming',
          statusClass: 'status-upcoming',
          statusText: '待办'
        });
      }
      return event;
    });
    
    this.setData({
      events: events
    });
    
    this.applyFilter();
    this.updateCounts();
    
    wx.showToast({
      title: '已撤销完成状态',
      icon: 'none'
    });
  },

  // 提醒方式变更
  onReminderTypeChange: function(e) {
    this.setData({
      reminderType: e.detail.value
    });
  },

  // 提醒时间变更
  onReminderTimeChange: function(e) {
    this.setData({
      reminderTime: e.detail.value
    });
  },

  // 保存提醒设置
  onSaveReminderSettings: function() {
    wx.showToast({
      title: '提醒设置已保存',
      icon: 'success'
    });
    
    // 这里可以添加实际的设置保存逻辑
    console.log('提醒设置：', {
      type: this.data.reminderType,
      time: this.data.reminderTime
    });
  }
});